﻿using Common.Contract;
using Common.Contract.ProductHandbook.Request;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;

namespace CrossSelling.Core.ServiceCommand
{
    public class UpdateProductHandbookProductDetailServiceCommand : ProviderServiceCommand<IProviderFactory, UpdateProductHandbookProductDetailRequest, ResponseRecord>
    {
        public UpdateProductHandbookProductDetailServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(UpdateProductHandbookProductDetailRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.Record == null)
            {
                throw new ArgumentException("Record must be set", "request");
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            bool isUpdate = false;
            var record = Request.Record;
            ProductHandbookProductDetail lastChange = Providers.ProductHandbooks.RetrieveProductHandbookProductDetail(record.ProductId);

            if (lastChange != null)
            {
                isUpdate = Providers.ProductHandbooks.UpdateProductHandbookDetail(record);
            }
            response.IsSuccess = isUpdate;
            return isUpdate;
        }
    }
}
